
describe('W3CPlayer.coffee', ->
  controller_stub = null
  user_stub = null
  target = null
  init = false

  beforeEach ->
    # do nothing
    if !init
      controller_stub = new Controller()
      user_stub = new UserInfo()
      tunelist_stub = new TuneList()
      target = new W3CPlayer(controller_stub)

      user_stub.user_id = 123
      user_stub.session_id = 456
      user_stub.user_name = 'test_user'
      controller_stub.user_me = user_stub
      
      init = true


  describe('W3CPlayer class ', ->
    it 'constructs', ->
      expect(target.context instanceof webkitAudioContext).toEqual(true)
      expect(target.publisher_node).not.toBeNull()
      expect(target.subscriber_node).not.toBeNull()
      expect(target.channel_merger).not.toBeNull()

    it 'can play as publisher', ->
      waitsFor(->
        return _audio_data_1_selected;
      , 'Timeout : you did not select sounds file', 30000);

      runs(->
        tune_test1 = new Tune(_audio_data_1, 123)      
        # set mock function
        spyOn(controller_stub, 'send_start_playing_request').andCallFake( (tune)->
          return
        )
        spyOn(controller_stub, 'msg_started_play').andCallFake( (tune)->
          return
        )
        spyOn(controller_stub, 'send_audio_stream').andCallFake( (pcm_block_allch)->
          return
        )
        spyOn(target, 'stop_publisher_node').andCallThrough()
        
        # to play
        finished = false
        try
          target.start_play_as_publisher(tune_test1, ->
              finished = true
            )
          
          expect(target.tune_list.tune_on_playing).toBe(tune_test1)
          expect(target.tune_list.tune_on_playing.requester_session_id).toEqual(user_stub.session_id)

          waitsFor(->
            return target.state is 'idle' or finished;
          , 'Timeout to play as publisher!!', 10000);

          runs(->
            expect(controller_stub.send_start_playing_request).toHaveBeenCalled()
            expect(controller_stub.msg_started_play).toHaveBeenCalled()
            expect(controller_stub.send_audio_stream).toHaveBeenCalled()
            expect(target.stop_publisher_node).toHaveBeenCalled()
          )

        catch error
          expect(false).toBeTruthy()


    it 'can play as subscriber', ->
      waitsFor(->
        return _audio_data_1_selected;
      , 'Timeout : you did not select sounds file', 30000);



          
      )
  )
) 
